package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Providerpharmacy;

@Repository(value = "providerPharmacyDao")
@Transactional(readOnly = false)
public class ProviderPharmacyDaoImpl implements ProviderPharmacyDao {
	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	} 
	
	public List<Providerpharmacy> getListProviderPharmacy() {
		TypedQuery<Providerpharmacy> providers = em.createNamedQuery("Providerpharmacy.findAll", Providerpharmacy.class);
    	return providers.getResultList();
	}
	
	public List<Providerpharmacy> getListProviderPharmacy(Integer pharmacyId, boolean active) {
		TypedQuery<Providerpharmacy> providers = em.createNamedQuery("Providerpharmacy.findByPharmacyIdByActive", Providerpharmacy.class);
		providers.setParameter("pharmacyId",pharmacyId);
		providers.setParameter("active",active);
    	return providers.getResultList();
	}
	
	public void add(Providerpharmacy providerPharmacy) {
		em.persist(providerPharmacy);
	}
	public void update(Providerpharmacy providerPharmacy) {
		em.merge(providerPharmacy);
	}
	public Providerpharmacy findProviderPharmacy(Integer pharmacyId,Integer providerId) {
		Providerpharmacy res = null;
    	Query  q = em.createNamedQuery("Providerpharmacy.findByProviderIdByPharmacyId");
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("providerId",providerId);
    	if(q.getResultList().size()==1)
    		res=(Providerpharmacy)q.getSingleResult();
    	return res;
	}

}
